package com.quanshu.exchange.ui.gui.message.group;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 群简介
 * Created by xiniu003 on 2017/12/8.
 */

public class GroupIntroActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.comment_group_intro)
    LinearLayout commentGroupIntro;
    @BindView(R.id.group_intro_input)
    EditText groupIntroInput;
    @BindView(R.id.input_length)
    TextView inputLength;

    private String primevalStr;
    private String groupIntroInputStr;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_intro);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        primevalStr = intent.getStringExtra("primeval");

        initView();
        initListener();
    }

    private void initView() {

        if (!TextUtils.isEmpty(primevalStr)) {
            groupIntroInput.setText(primevalStr);
            groupIntroInput.setSelection(primevalStr.length());
            groupIntroInput.requestFocus();
        }
    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
        RxViewHelper.getInstance().clicks(commentGroupIntro, aVoid -> {

            if (!TextUtils.isEmpty(groupIntroInputStr)) {
                Intent intent = new Intent();
                intent.putExtra("bian", groupIntroInputStr);
                setResult(RESULT_OK,intent);
                finish();
            } else {
                ToastHelper.toast("您还未输入群简介");
            }


        });

        RxViewHelper.getInstance().afterTextChangeEvents(groupIntroInput,
                textViewAfterTextChangeEvent -> {

                    groupIntroInputStr = textViewAfterTextChangeEvent.editable().toString();

                    if (!TextUtils.isEmpty(groupIntroInputStr)) {

                        inputLength.setText(groupIntroInputStr.length() + "/100");
                    } else {
                        inputLength.setText("0" + "/100");
                    }

                });

    }

}
